package com.bytz.modules.cms.order.controller;

import com.bytz.common.api.vo.Result;
import com.bytz.common.aspect.annotation.AutoLog;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.order.entity.OrderFlowRule;
import com.bytz.modules.cms.order.model.OrderDesignFlowRuleListVo;
import com.bytz.modules.cms.order.model.OrderDesignFlowRuleSaveRo;
import com.bytz.modules.cms.order.model.OrderDesignFlowRuleTestIn;
import com.bytz.modules.cms.order.model.OrderDesignFlowRuleTestOut;
import com.bytz.modules.cms.order.service.IOrderDesignFlowRuleService;
import com.bytz.modules.cms.order.service.IOrderFlowRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "订单流程规则设计")
@RequestMapping("/orderDesignFlowRule")
public class OrderDesignFlowRuleController {

    @Autowired
    private IOrderDesignFlowRuleService orderDesignFlowRuleService;

    @Autowired
    private IOrderFlowRuleService orderFlowRuleService;

    /**
     * 分页列表查询
     */
    @ApiOperation(value = "查询全部脚本")
    @GetMapping(value = "/getAll")
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderDesignFlowRule.VIEW + "')")
    public Result<OrderDesignFlowRuleListVo> getAll() {
        return Result.ok(orderDesignFlowRuleService.getAll());
    }

    /**
     * 查询全部发布了的脚本
     */
    @ApiOperation(value = "查询全部发布了的脚本")
    @GetMapping(value = "/getAllPublished")
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderDesignFlowRule.VIEW + "')")
    public Result<List<OrderFlowRule>> getAllPublished() {
        return Result.ok(orderFlowRuleService.getAll());
    }

    /**
     * 保存
     *
     * @param saveRo 规则
     */
    @AutoLog(value = "保存")
    @ApiOperation(value = "保存")
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderDesignFlowRule.EDIT + "')")
    @PostMapping(value = "/save")
    public Result<Boolean> save(@RequestBody @Valid OrderDesignFlowRuleSaveRo saveRo) {
        orderDesignFlowRuleService.updateSaveObject(saveRo);
        return Result.ok("保存成功！");
    }

    /**
     * 保存
     *
     * @param saveRo 规则
     */
    @AutoLog(value = "发布")
    @ApiOperation(value = "发布")
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderDesignFlowRule.EDIT + "')")
    @PostMapping(value = "/publish")
    public Result<Boolean> publish(@RequestBody @Valid OrderDesignFlowRuleSaveRo saveRo) {
        orderDesignFlowRuleService.publish(saveRo);
        return Result.ok("保存成功！");
    }


    /**
     * 测试脚本
     */
    @ApiOperation(value = "测试脚本")
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderDesignFlowRule.VIEW + "')")
    @PostMapping(value = "/test")
    public Result<OrderDesignFlowRuleTestOut> test(@RequestBody @Valid OrderDesignFlowRuleTestIn ruleTestVo) throws Exception {
        return Result.ok(orderDesignFlowRuleService.testScript(ruleTestVo));
    }
}
